const x = [
    {
        "name": "帧头",
        "bit_length": "16",
        "type": "uint",
        "default": "0xeb90",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    },
    {
        "name": "源地址",
        "bit_length": "8",
        "type": "uint",
        "default": "0x22",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    },
    {
        "name": "目的地址",
        "bit_length": 8,
        "type": "uint",
        "default": "0xdd",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": {
            "0xdd": "光学头1电机",
            "0xee": "光学头2电机"
        }
    },
    {
        "name": "数据长度",
        "bit_length": "8",
        "type": "uint",
        "default": "0x10",
        "min": "0x10",
        "max": "0x10",
        "handler_func": null,
        "enum": null
    },
    {
        "name": "指令码",
        "bit_length": "8",
        "type": "uint",
        "default": "0xe4",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    },

    {
        "name": "电机类型",
        "bit_length": 8,
        "type": "uint",
        "default": "0x55",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": {
            "0x55": "方位电机",
            "0xaa": "俯仰电机",
            "0xbb": "两个电机同时"
        }
    },
    {
        "name": "方位电机速度",
        "bit_length": 32,
        "type": "uint",
        "default": 12333,
        "min": -2147483648,
        "max": 2147483647,
        "handler_func": null,
        "enum": null,
        "status": "success"
    },
    {
        "name": "俯仰电机速度",
        "bit_length": 32,
        "type": "uint",
        "default": -12333,
        "min": -2147483648,
        "max": 2147483647,
        "handler_func": null,
        "enum": null,
        "status": "success"
    },
    {
        "name": "预留",
        "bit_length": 48,
        "type": "uint",
        "default": 0,
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null,
        "status": "success"
    },
    {
        "name": "校验和",
        "bit_length": "8",
        "type": "uint",
        "default": "0xF3",
        "min": 0,
        "max": 0,
        "handler_func": null,
        "enum": null
    },
    {
        "name": "帧尾",
        "bit_length": "16",
        "type": "uint",
        "default": "0x09d7",
        "min": null,
        "max": null,
        "handler_func": null,
        "enum": null
    }
]

function hexStringToUint8Array(hexString) {
    hexString = hexString.replace("0x", '')
    // 确保十六进制字符串是偶数长度
    if (hexString.length % 2 !== 0) {
        hexString = '0' + hexString;
    }

    // 拆分成两个字符一组
    const byteArray = new Uint8Array(hexString.length / 2);
    for (let i = 0; i < hexString.length; i += 2) {
        byteArray[i / 2] = parseInt(hexString.substring(i, i + 2), 16);
    }

    return byteArray;
}

const xx = x.map(i => {
    if (typeof i.default === "number") {
        return i.default.toString(16).padStart(i.bit_length / 4, "0");
    }
    return i.default.padStart(i.bit_length / 4, "0")
})
//
// const arr = []
// for (let uint of xx) {
//     arr.push(...hexStringToUint8Array(uint))
//     console.log(hexStringToUint8Array(uint));
// }
// console.log(new Uint8Array(arr).buffer)

export function generateUint8Array(params) {
    let arr = []
    // 预处理, 10 -> 16转换
    let hexArr = params.slice(4,-3).map((item, i) => {
        // 获取值和位数
        let {default: defaultValue, bit_length} = item;
        // 去除0x
        defaultValue = typeof defaultValue === "string" ? defaultValue.replace(/^0x/, "") : defaultValue;
        if (typeof defaultValue === "number") {
            return defaultValue.toString(16).padStart(bit_length / 4, "0")
        }
        return defaultValue.padStart(bit_length / 4, "0")
        // 去除0x
    }).map((hexStr) => hexStr.replace("0x", ""))
    hexArr.forEach((hexString) => {
        // 确保十六进制字符串是偶数长度
        if (hexString.length % 2 !== 0) {
            hexString = '0' + hexString;
        }

        // 逐个字节拆分
        const byteArray = new Uint8Array(hexString.length / 2);
        for (let i = 0; i < hexString.length; i += 2) {
            byteArray[i / 2] = parseInt(hexString.substring(i, i + 2), 16);
        }
        arr.push(...byteArray)
    })
    return new Uint8Array(arr);
}
function calculateChecksum(bytes) {
    console.log(bytes)
	// 计算字节和
	let sum = bytes.reduce((acc, byte) => acc + byte, 0);

	// 取低8位 (sum % 256)
	let checksum = sum & 0xff;

	// 返回校验和，转换成2位的16进制字符串
	return checksum.toString(16).padStart(2, '0').toUpperCase();
}

console.log(calculateChecksum(generateUint8Array(x)))